#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>

#ifndef BUFSIZ
#define BUFSIZ 1024
#endif

struct ll_node {
	int passenger_id;
	struct ll_node *next;
};

int
main(int argc, char **argv)
{
	struct ll_node **to, **to_tail, *tmp_ll_node;
	register int i;
	int n, passenger_id = 1, cash_reg_id;
	static char buf[BUFSIZ], cmd[BUFSIZ];

	scanf("%d", &n);
	to = calloc((size_t) n, sizeof *to);
	to_tail = calloc((size_t) n, sizeof *to);

	while (fgets(buf, BUFSIZ, stdin)) {
		if (sscanf(buf, "%d %[^\n]\n", &cash_reg_id, cmd) == 1) {
			assert(cash_reg_id == 0);
			break;
		}
		--cash_reg_id;

		if (strcmp(cmd, "queued up") == 0) {
			if (!to_tail[cash_reg_id]) {
				assert(to[cash_reg_id] == NULL);
				to_tail[cash_reg_id] = to[cash_reg_id] =
					calloc(1UL, sizeof (struct ll_node));
			} else {
				assert(to_tail[cash_reg_id]->next == NULL);
				to_tail[cash_reg_id]->next = calloc(1UL, sizeof (struct ll_node));
				to_tail[cash_reg_id] = to_tail[cash_reg_id]->next;
			}
			to_tail[cash_reg_id]->passenger_id = passenger_id++;
		} else if (strcmp(cmd, "served") == 0) {
			printf("%d\n", to[cash_reg_id]->passenger_id);

			if (to[cash_reg_id] == to_tail[cash_reg_id])
				to_tail[cash_reg_id] = NULL;

			tmp_ll_node = to[cash_reg_id];
			to[cash_reg_id] = to[cash_reg_id]->next;
			free(tmp_ll_node);
		}
	}

	for (i = 0; i < n; i++) {
		if (to[i]) {
			assert(to_tail[i] && to_tail[i]->next == NULL);
			while (to[i]) {
				tmp_ll_node = to[i];
				to[i] = to[i]->next;
				free(tmp_ll_node);
			}
		}
	}
	free(to);
	free(to_tail);

	return 0;
}
